// Environment library
var lerror="Chưa có lỗi nào !";
var messageschedule;
var lstatus="";
//----------------------------------------------------------------------------------------------------------
function msgbox(status,msg)
{
	var smoptions = {id: (status=="fail")?"loginerror":"loginok",
		   position: "bottom",
		   size: 50,
		   backgroundColor: (status=="fail")?"red":"green",
		   delay: 1500,
		   speed: 500,
		   fontSize: "30px"
	 };
	$.showMessage(msg, smoptions);
	if(status=="fail")
	lerror=msg;
}
//----------------------------------------------------------------------------------------------------------
function showLastError()
{
	var smoptions = {id: "loginerror",
		   position: "bottom",
		   size: 50,
		   backgroundColor: "red",
		   delay: 1500,
		   speed: 500,
		   fontSize: "30px"
	 };
	$.showMessage(lerror, smoptions);
}
//----------------------------------------------------------------------------------------------------------
function buildAVIM()
{
	$("#AVIMControl").dialog("close");
	$("#AVIMControl").remove();
	$("body").append('<div id="AVIMControl"></div>');
	$("#AVIMControl").append('<p class="AVIMControl"><input id="avim_auto" type="radio" name="AVIMMethod" onclick="AVIMObj.setMethod(0);" />Tự động');
	$("#AVIMControl").append('<input id="avim_telex" type="radio" name="AVIMMethod" onclick="AVIMObj.setMethod(1);" />TELEX');
	$("#AVIMControl").append('<input id="avim_vni" type="radio" name="AVIMMethod" onclick="AVIMObj.setMethod(2);" />VNI');
	$("#AVIMControl").append('<input id="avim_viqr" type="radio" name="AVIMMethod" onclick="AVIMObj.setMethod(3);" />VIQR');
	$("#AVIMControl").append('<input id="avim_viqr2" type="radio" name="AVIMMethod" onclick="AVIMObj.setMethod(4);" />VIQR*');
	$("#AVIMControl").append('<input id="avim_off" type="radio" name="AVIMMethod" onclick="AVIMObj.setMethod(-1);" />Tắt<br />');
	$("#AVIMControl").append('<a class="AVIMControl" style="float: right; position: relative; top: 3px;" onclick="document.getElementById(' + "'AVIMControl').style.display='none';" + '">[Ẩn AVIM - F12]</a>');
	$("#AVIMControl").append('<input type="checkbox" id="avim_ckspell" onclick="AVIMObj.setSpell(this);" />Chính tả');
	$("#AVIMControl").append('<input type="checkbox" id="avim_daucu" onclick="AVIMObj.setDauCu(this);" />Kiểu cũ</p>');
	$("#AVIMControl").dialog({
		title:"Bộ gõ tiếng Việt",
		width:200,
		position:["right","top"],
		stack:true,
		beforeClose:function(){
			return false;
		}
	});
	AVIMObj.setMethod(1);
}
//----------------------------------------------------------------------------------------------------------
function builToolbar()
{
	$("body").append("<div id=\"toolbardiv\"></div>");
	$("#toolbardiv").load("modules/toolbar/toolbar.php");
}
//----------------------------------------------------------------------------------------------------------
function buildClock()
{
	$("#clockdiv").dialog("close");
	$("#clockdiv").remove();
	var skins=["swissRail","chunkySwiss","fancy","machine","classic","modern","simple"];
	var i=parseInt(Math.floor((Math.random()*skins.length)));
	var askin=skins[i];
	
	$("body").append("<div id=\"clockdiv\"><canvas id=\"clock1\" class=\"CoolClock:"+askin+":::+7\"></canvas></div>");
	$("#clockdiv").dialog({
		title:"Đồng hồ",
		width:200,
		position:["right","center"],
		stack:true,
		beforeClose:function(){
			return false;
		}
	});
	CoolClock.findAndCreateClocks();
}
//----------------------------------------------------------------------------------------------------------
function buildMenu()
{
	$("#fmenu").remove();
	$("body").append("<div id=\"fmenu\" style=\"z-index:2147483640;\"></div>");
	$("#fmenu").load("modules/appmenu/menus.php");
}
//----------------------------------------------------------------------------------------------------------
function buildEnvironment()
{
	buildMenu();
	buildAVIM();
	builToolbar();
	buildClock();
}
//----------------------------------------------------------------------------------------------------------
function ajaxSuccess(xml)
{
	var status=$(xml).find("status").text();
	var message=$(xml).find("message").text();
	msgbox(status,message);
	if(status=="ok")
	{
		$("#flogin").dialog("close");
		$("#flogin").remove();
		buildEnvironment();
	}
}
//----------------------------------------------------------------------------------------------------------
function ajaxLogin()
{
	var un1=$("#un").val();
	var pwd1=$("#pwd").val();
	$.ajax({
		url:"modules/login/dologin.php",
		type:"post",
		dataType:"xml",
		data:"un="+encodeURI(un1)+"&pwd="+encodeURI(pwd1),
		success:ajaxSuccess
	});
}
//----------------------------------------------------------------------------------------------------------
function buildLerror()
{
	$("#divlerror").dialog("close");
	$("#divlerror").remove();
	$("body").append("<div id=\"divlerror\" style=\"display:none;\"><img src=\"images/tools/error.jpg\" width=\"100px\" height=\"100px\" id=\"imglerror\" title=\"Hiển thị thông báo lỗi trước !\" /></div>");
	$("#divlerror").dialog({
		position:["right","bottom"],
		title:"Lỗi trước",
		minWidth:130,
		maxWidth:130,
		minHeight:150,
		maxHeight:150,
		width:130,
		height:150,
		stack:true,
		beforeClose:function(){return false;}
	});
	$("#imglerror").click(function(){
		showLastError();
	});
}
//----------------------------------------------------------------------------------------------------------
function buildLogin()
{
	$("body").append("<div id=\"flogin\"></div>");
	$("#flogin").load("modules/login/login.php","",function(){
		$("#un").keyboard();
		$("#pwd").keyboard();
	});
	
	var floginsettings={
		title:"Đăng nhập",
		width:420,
		maxWidth:420,
		minWidth:420,
		buttons:{"Đăng nhập":function(){ajaxLogin();}},
	};
	$("#flogin").dialog(floginsettings);
}
//----------------------------------------------------------------------------------------------------------
function rebuildACL()
{
	buildAVIM();
	buildClock();
	buildLerror();
}
//----------------------------------------------------------------------------------------------------------
function disableRightClick()
{
	$(document).bind("contextmenu", function(e) {
                e.preventDefault();
    });
}
//----------------------------------------------------------------------------------------------------------
$(document).ready(function(){
	buildLerror();
	buildLogin();
	disableRightClick();
});
//----------------------------------------------------------------------------------------------------------
